using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
//using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using TDBaseDataLibrary;

namespace TDFlowLibrary
{
    public enum pgmstate
    {
        state_begine = 0,
        state_mainmenu = 1,
        state_mapedit = 2,
        state_level = 3,
        state_intrrput = 4,
    };

    public class SceneItem
    {
        public String strName;
        public TDBaseScene Scene;
    }
    public interface gameflow_Decision
    {
        int OnUpdateRet(gameflow flow, SceneItem Item, int nIndex, int nRet);
    }
    public class gameflow
    {
        gameflow_Decision m_Interface;
        //public Stack<pgmstate> m_gameState;
        public Stack<int> m_gameState;
        public List<SceneItem> m_SceneList;
        //MainMenu    m_mainMenu;
        //LevelSystem m_LevelSystem;

        public gameflow()
        {
            m_SceneList = new List<SceneItem>();
            m_gameState = new Stack<int>();
        }
        ~gameflow()
        {
        }

        public void AddScene(String StrName,TDBaseScene pScene)
        {
            SceneItem   Item = new SceneItem();
            Item.strName = StrName;
            Item.Scene = pScene;
            m_SceneList.Add(Item);
        }
        public void SetInterface(gameflow_Decision funcallback)
        {
            m_Interface = funcallback;
        }
        public bool init()
        {
            return true;
        }
        public int setstate(int state)
        {
            int s = m_gameState.Pop();
            m_gameState.Push(s);
            return s;
        }
        public void pushstate(int state)
        {
            m_gameState.Push(state);
        }
        public int popstate()
        {
            return m_gameState.Pop();
        }
        public void LoadContent(ContentManager Content)
        {
            for (int i = 0; i < m_SceneList.Count; i++)
            {
                m_SceneList[i].Scene.LoadContent(Content);
            }
            //m_mainMenu.LoadContent(Content);
            //m_LevelSystem.LoadContent(Content);
            //m_LevelSystem.InitLevelSystem(Content, "Levels/Level_Info");
        }
        public bool Update(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            if (m_gameState.Count() == 0)
                return true;
            int s = m_gameState.Peek();
            int nRet = m_SceneList[s].Scene.Update(graphics, gameTime);
            if (m_Interface != null)
                m_Interface.OnUpdateRet(this, m_SceneList[s], s, nRet);
            //switch (s)
            //{
            //    case pgmstate.state_mainmenu:
            //        {
            //            m_mainMenu.Update(graphics, gameTime);
            //            if (m_mainMenu.command == mainmenu_command.mainmenu_level)
            //            {
            //                m_LevelSystem.LoadNextLevel(GameGlobalInfo.m_Game.Content);
            //                setstate(pgmstate.state_level);
            //                m_mainMenu.command = mainmenu_command.mainmenu_normal;
            //            }
            //            else if (m_mainMenu.command == mainmenu_command.mainmenu_exit)
            //            {
            //                return false;
            //            }
            //        }
            //        break;
            //    case pgmstate.state_level:
            //        m_LevelSystem.Update(graphics, gameTime);
            //        break;
            //}
            return true;
        }
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int xOffset, int yOffset)
        {
            if (m_gameState.Count == 0)
                return;
            int s = m_gameState.Peek();
            m_SceneList[s].Scene.Draw(graphics, gameTime, spriteBatch, xOffset, yOffset, 1.0f);
        }
    }
}
